package com.etooling.framework.entity;


import com.etooling.framework.svo.AbstractEntity;
import com.etooling.framework.utils.WebUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 企业内部的角色分工
 */
@Entity
@Table(name = "t_role", schema = "framework")
public class Role extends AbstractEntity {
    final public static String RUNNING_STATUS = "启用";
    final public static String STOP_STATUS = "停用";
    String name;
    String roleCode;
    List<FuncModule> funcModules;
    List<FuncModule> roleIndexModules;//角色默认打开的页
    AttachFile avatar;//头像
    String status;

    public String getRoleCode() {
        return roleCode;
    }

    public void setRoleCode(String roleCode) {
        this.roleCode = roleCode;
    }

    public Role() {
        super();
    }

    public Role(String id) {
        this.setId(id);
    }

    @Transient
    List<String> funcModuleIds;


    @Transient
    public List<String> getFuncModuleIds() {
        List<String> ls = new ArrayList<>();
        for (FuncModule model : funcModules) {
            ls.add(model.getId());
        }
        return ls;
    }

    @Transient
    public void setFuncModuleIds(List<String> funcModuleIds) {
        List<String> ls = new ArrayList<>();
        if (WebUtils.checkStr(funcModules) && funcModules.size() > 0) {
            for (FuncModule model : funcModules) {
                ls.add(model.getId());
            }
        }
        funcModuleIds = ls;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }


    @ManyToOne(targetEntity = AttachFile.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "avatar_file_id")
    public AttachFile getAvatar() {
        return avatar;
    }

    public void setAvatar(AttachFile avatar) {
        this.avatar = avatar;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)

    @JoinTable(name = "t_role_func", schema = "framework", joinColumns = {@JoinColumn(name = "role_id")}, inverseJoinColumns = {
            @JoinColumn(name = "func_id")})

    public List<FuncModule> getFuncModules() {
        return funcModules;
    }

    public void setFuncModules(List<FuncModule> funcModules) {
        this.funcModules = funcModules;
    }

    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinTable(name = "t_role_index_module", schema = "framework", joinColumns = {@JoinColumn(name = "role_id")}, inverseJoinColumns = {
            @JoinColumn(name = "func_id")})

    public List<FuncModule> getRoleIndexModules() {
        return roleIndexModules;
    }

    public void setRoleIndexModules(List<FuncModule> roleIndexModules) {
        this.roleIndexModules = roleIndexModules;
    }
}